package com.ruoyi.web.controller.student_club.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.student_club.domain.User;
import com.ruoyi.student_club.dto.UpdateUserDto;
import com.ruoyi.student_club.dto.UserDto;
import com.ruoyi.student_club.mapper.UserMapper;
import com.ruoyi.student_club.service.IUserService;
import com.ruoyi.student_club.utils.GetOpenIdUtil;
import com.ruoyi.student_club.vo.app.UserInfoVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.rmi.server.ExportException;

/**
 * @Author lin
 * @Date 2022/2/18 11:25
 */
@RestController
@RequestMapping("/wechat")
public class LoginController {

    @Resource
    private IUserService userService;

    /**
     * 获取openId
     * @param code 授权码
     * @return
     */
    @GetMapping("/getOpenId")
    public AjaxResult getOpenId(String code) throws IOException {
        UserInfoVo userInfo = userService.saveOpenId(code);
        return AjaxResult.success(userInfo);
    }

    /**
     * 保存用户头像和昵称
     * @param dto 信息对象
     * @return
     */
    @PostMapping("/updateUserInfo")
    public AjaxResult update(@RequestBody UserDto dto){
        userService.updateUserInfo(dto);
        return AjaxResult.success();
    }

    /**
     * 获取我的资料
     * @param openId 用户标识
     * @return
     */
    @GetMapping("/getUserInfo")
    public AjaxResult getUserInfo(String openId){
        User userInfo = userService.getUserInfo(openId);
        return AjaxResult.success(userInfo);
    }

    /**
     * 更新资料
     * @param dto
     * @return
     */
    @PostMapping("/update")
    public AjaxResult updateUser(@RequestBody UpdateUserDto dto){
        userService.updateUserInfo(dto);
        return AjaxResult.success();
    }
}
